package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.redis.service.IUserinfoRedisService;
import cn.wolfcode.wolf2w.repository.UserInfoRepository;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.AssertUtil;
import cn.wolfcode.wolf2w.util.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private UserInfoRepository repository;
    @Autowired
    private IUserinfoRedisService redisService;

    @Override
    public void update(UserInfo userInfo) {
        repository.save(userInfo);
    }

    @Override
    public void save(UserInfo userInfo) {
        repository.save(userInfo);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public UserInfo get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<UserInfo> list() {
        return repository.findAll();
    }

    @Override
    public boolean findByPhone(String phone) {
        UserInfo byPhone = repository.findByPhone(phone);
        return byPhone == null;
    }

    @Override
    public void sendMessages(String phone) {
        //1:创建验证码, 编辑短信
        String code = UUID.randomUUID().toString().
                replaceAll("-", "").substring(0, 4);
        StringBuffer message = new StringBuffer();
        message.append("亲爱的手机用户：").append(phone)
                .append("你好，您的验证码是：").append(code).append("。/n验证码将会在")
                .append(Consts.VERIFY_CODE_VAI_TIME).append("分钟后失效！");
        System.out.println(message);
//————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
//        SpringBoot里面给了我们能在Java里面发送请求
//        RestTemplate template = new RestTemplate();
//        //参数一：url
//        String url = "https://way.jd.com/chuangxin/dxjk?mobile={1}&content=【创信】{2}&appkey=26d41c91083fefcf07495518c4407ffe";
//        //参数二：接口的返回值
//        String forObject = template.getForObject(url, String.class, phone, message.toString());
//        System.out.println(forObject);
//        if (!forObject.contains("Success")) {
//            throw new LogicException("验证码发送失败！");
//        }
        redisService.savePhonekey(phone, code);
    }

    @Override
    public void register(String phone, String nickname, String password, String rpassword, String verifyCode) {
        AssertUtil.hasLength(phone, "手机号码不能为空");
        AssertUtil.hasLength(nickname, "昵称不能为空");
        AssertUtil.hasLength(password, "密码不能为空");
        AssertUtil.hasLength(rpassword, "密码不能为空");
        AssertUtil.hasLength(verifyCode, "验证码不能为空");
        AssertUtil.isEquals(password, rpassword, "两次密码不一致！");
        //手机号码是否正确
        System.out.println("——————————————————————————————————————————————————————————————————————");
        //手机号码是否唯一
        if (!this.findByPhone(phone)) {
            throw new LogicException("该手机已经被注册");
        }
        //验证码
        String code = redisService.getVerifyCode(phone);
        if (!verifyCode.equalsIgnoreCase(code)) {
            throw new LogicException("验证码无效！");
        }
        //注册
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname(nickname);
        userInfo.setPhone(phone);
        userInfo.setEmail("");
        userInfo.setPassword(password);
        userInfo.setLevel(1);
        userInfo.setInfo("");
        userInfo.setCity("");
        userInfo.setHeadImgUrl("/images/default.jpg");
        //核心属性必须自己控制！
        userInfo.setState(UserInfo.STATE_NORMAL);
        this.save(userInfo);
    }

    @Override
    public UserInfo login(String username, String password) {
        UserInfo byPhone = repository.findByPhone(username);
        if(byPhone==null || !byPhone.getPassword().equals(password)){
            throw new LogicException("密码错误或没有这个账户！");
        }
        return byPhone;
    }

    @Override
    public List<UserInfo> getByCity(String keyword) {
        return repository.findByCity(keyword);
    }
}
